﻿namespace Dcms.BaseClass
{
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;
    using System.Web;
    using System.Web.Caching;

    public class DcmsAuth
    {
        public static System.Web.Caching.Cache Cache = HttpRuntime.Cache;
        private static byte[] Keys = new byte[] { 0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef };

        public static string certifyReturnArray(string decryptString)
        {
            try
            {
                string decryptKey = "DcmsTank";
                decryptString = decryptString.Substring(10);
                byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                string firstString = Encoding.UTF8.GetString(mStream.ToArray());
                string dtext = "";
                for (int i = 0; i < firstString.Length; i++)
                {
                    dtext = dtext + ((char) (firstString[i] - '\n' + 2));
                }
                return dtext;
            }
            catch
            {
                return "||";
            }
        }

        public static string GetCache(string strName)
        {
            if (Cache[strName] == null) return "";
            return Cache[strName].ToString();
        }

        public static string GetHost()
        {
            return HttpContext.Current.Request.Url.Host.ToUpper();
        }

        public static bool isReg()
        {
            return Validate(GetCache("snCode"));
        }

        public static bool Validate(string encryptString)
        {
            bool RegValue = false;
            string[] regStrArr = certifyReturnArray(encryptString).Split(new char[] { '|' });
            string RootDomain = regStrArr[0].ToUpper().Trim();
            string beginTime = regStrArr[1].Trim();
            string years = regStrArr[2].Trim();
            DateTime BeginTime = Convert.ToDateTime("2000-10-22");
            try
            {
                BeginTime = Convert.ToDateTime(beginTime);
            }
            catch
            {
                BeginTime = Convert.ToDateTime("2000-10-22");
            }
            int Years = 1;
            try
            {
                Years = Convert.ToInt32(years);
            }
            catch
            {
                Years = 1;
            }
            int ValueDays = Years * 0x16d;
            TimeSpan Ts = (TimeSpan) (DateTime.Now - BeginTime);
            int LetterNum = GetHost().Length - RootDomain.Length;
            if (ValueDays - Ts.Days >= 0 && GetHost().IndexOf(RootDomain) != -1 && LetterNum >= 0 && RootDomain.Length > 3)
            {
                RegValue = true;
                Cache.Insert("isReg", RegValue);
            }
            return RegValue;
        }

        public static void ValidateAndRedirect()
        {
            if (!isReg())
            {
                HttpContext.Current.Response.Redirect("~/System/Validate.aspx");
                HttpContext.Current.Response.End();
            }
        }
    }
}

